package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.Lifecycle
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.adapter.live.LiveBannerAbsAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.WidgetLiveAbsBannerBinding
import com.polaris.live.resp.back_resp.BannerBean
import com.polaris.live.utils.route.RouterUtils
import com.zhpan.indicator.enums.IndicatorSlideMode

/**
 * LiveAbsBannerView  直播广告轮播图
 *
 * @author Created by 半仙 on 2023/11/11/011 9:59
 */
class LiveAbsBannerView(context: Context, attributeSet: AttributeSet) :
    LinearLayout(context, attributeSet) {

    val mBinding = WidgetLiveAbsBannerBinding.inflate(LayoutInflater.from(context), this)

    private val mBannerAdapter by lazy { LiveBannerAbsAdapter() }

    init {
        orientation = VERTICAL
    }

    fun setAbsBanner(bannerBeans: List<BannerBean>, viewLifecycleOwner: Lifecycle) {
        mBinding.bannerView.apply {
            registerLifecycleObserver(viewLifecycleOwner)
            setAdapter(mBannerAdapter)

            setIndicatorVisibility(View.GONE)
            setIndicatorSlideMode(IndicatorSlideMode.SMOOTH)
            setIndicatorView(mBinding.indicatorView)
            setIndicatorSliderColor(
                ColorUtils.getColor(R.color.color_ffffff_30),
                ColorUtils.getColor(R.color.white)
            )
            setIndicatorSliderWidth(3.dp)
            setOnPageClickListener { _, position ->
                val itemData = bannerBeans.getOrNull(position) ?: return@setOnPageClickListener
                val link = itemData.link.takeIf { it?.isNotEmpty() == true }
                    ?: itemData.nativeLink.takeIf { it?.isNotEmpty() == true }
                link?.let { RouterUtils.routerGoto(it) }
            }
            setScrollDuration(500)
            create(bannerBeans)
        }
    }
}